package com.jzo2o.foundations.service.impl;

import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.M
 * @version 1.0
 * @description 首页缓存服务类
 * @date 2024/11/4 15:17
 */
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {

    @Resource
    private ServeMapper serveMapper;
    @Resource
    private IServeItemService serveItemService;
    @Resource
    private IServeService serveService;

    @Override
    @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", cacheManager = RedisConstants.CacheManager.FOREVER)
    public List<ServeCategoryResDTO> queryServeIconCategoryByRegionIdCache(Long regionId) {
        List<ServeCategoryResDTO> serveIconCategoryByRegionId = serveMapper.findServeIconCategoryByRegionId(regionId);

        //取出前两条
        List<ServeCategoryResDTO> serveCategoryResDTOS = new ArrayList<>(serveIconCategoryByRegionId.size()>=2?serveIconCategoryByRegionId.subList(0, 2):serveIconCategoryByRegionId);
        //遍历列表，每个元素中的服务项目列表只取出前四个
        serveCategoryResDTOS.forEach(serveCategoryResDTO -> {
            List<ServeSimpleResDTO> serveResDTOList = serveCategoryResDTO.getServeResDTOList();
            if(serveResDTOList.size()>=4){
                serveResDTOList = new ArrayList<>(serveResDTOList.subList(0, 4));
            }
            serveCategoryResDTO.setServeResDTOList(serveResDTOList);
        });

        return serveCategoryResDTOS;
    }

    /**
     * 根据区域id获取服务类型列表
     *
     * @param regionId
     * @return
     */
    @Override
    @Cacheable(value = RedisConstants.CacheName.SERVE_TYPE, key = "#regionId", cacheManager = RedisConstants.CacheManager.FOREVER)
    public List<ServeCategoryResDTO> serveTypeList(Long regionId) {
        List<ServeCategoryResDTO> serveCategoryResDTOS = serveMapper.serveTypeList(regionId);
        return serveCategoryResDTOS;
    }

    /**
     * 热门服务列表
     *
     * @param regionId
     * @return
     */
    @Override
    @Caching(
            cacheable = {
                    @Cacheable(value = RedisConstants.CacheName.HOT_SERVE, key = "#regionId",
                            unless = "#result.size() !=0", cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
                    @Cacheable(value =RedisConstants.CacheName.HOT_SERVE, key = "#regionId",
                            unless = "#result.size() ==0", cacheManager = RedisConstants.CacheManager.FOREVER)})
    public List<ServeAggregationSimpleResDTO> hotServeList(Long regionId) {
        List<ServeAggregationSimpleResDTO> serveAggregationSimpleResDTOS = serveMapper.hotServeList(regionId);
        return serveAggregationSimpleResDTOS;
    }

    /**
     * 服务详情
     *
     * @param serveId
     * @return
     */
    @Override
    public ServeAggregationSimpleResDTO serveDetail(Long serveId) {
        Serve serve = serveService.getById(serveId);
        ServeItem item = serveItemService.getById(serve.getServeItemId());
        ServeAggregationSimpleResDTO serveAggregationSimpleResDTO = BeanUtils.copyBean(serve, ServeAggregationSimpleResDTO.class);
        serveAggregationSimpleResDTO.setServeItemName(item.getName());
        serveAggregationSimpleResDTO.setServeItemImg(item.getImg());
        serveAggregationSimpleResDTO.setUnit(item.getUnit());
        serveAggregationSimpleResDTO.setDetailImg(item.getDetailImg());
        serveAggregationSimpleResDTO.setServeItemId(item.getId());
        return serveAggregationSimpleResDTO;
    }
}
